<?
/**
 * VoiceOne Settings/Extensions
 *
 * Frontend to provide access to the extensions system in voiceone.
 * @package voiceone
 * @subpackage  settings_extensions
 */
 
  include ("../../../admin/config/cfg.php");
  global $cwlang;

  //================ POST & GET =================
  $alert=getAlert();
  if($_GET["cmd"]=="del") {
    //delete extension
    $ret=$asterisk->delClient($_GET["id"]);
    //remove cid setting
    if ($ret==1){
      $ret=$asterisk->delCid(0,$_GET["id"]);
    }
    //set return
    if ($ret==1) $alert="<div class=\"AlertGood\">".T_("Extension successfully deleted")."</div>";
    else $alert="<div class=\"AlertBad\">".T_("Delete failed")."</div>";
    goToMainPage($alert,"./");
  }
  if($_GET["cmd"]=="del-virtual"){
    //delete extension
    $ret=$asterisk->delVirtualExtension($_GET["id"]);
    //set return
    if($ret>0) $alert="<div class=\"AlertGood\">".T_('Virtual Extension successfully deleted')."</div>";
    else $alert="<div class=\"AlertBad\">".T_('Delete failed')."</div>";
    goToMainPage($alert,"./");
  }
  
  //================ PAGE LOGIC ================
  $html="";
  $blacklist=array();
  //get virtual clients
  $ret=$asterisk->getVirtualExtension(0);
  if($err=$asterisk->getError()) die("Error: $err");
  $count=0; 
  if((is_array($ret))&&(count($ret)>0)){
    $html.="<td colspan=\"2\" class=\"todolisttitle\">".T_("Virtual Extensions")."</td></tr><tr>\n";
    foreach($ret as $v){
      $id=$v["id"];
      //put linked clients in black list 
      if(is_array($v["linked_clients"])) $blacklist=array_merge($blacklist,$v["linked_clients"]);
      //build html
      $virtual_name=$v["name"]." ".$v["surname"]."&lt;".$v["extension"]."&gt;";
      $virtual_icon="<img src=\"$relBasePath/public/img/virtual_extension.jpg\" />";
      $html.="<td style=\"vertical-align: top;\">";
      $html.="$virtual_icon <a href=\"edit-virtual/?id=$id\">$virtual_name</a>  <a href=\"$PHP_SELF?cmd=del-virtual&id=$id\" onclick=\"if (confirm('".T_('Account:')." ".sprintf(__('Are you sure you want to remove the virtual extension %s'),$ext )."')) return true; else return false;\"><img src=\"$relBasePath/public/img/trash.gif\" /></a>\n";
      if(is_array($v["linked_clients"])){
        $html.="<ul style=\"list-style:none; margin-left: 50px;padding-top: 0.5em;\">";
        foreach($v["linked_clients"] as $id_client){
        	$client=array();
          $client=getClient($id_client,0);
          if((is_array($client))&&(is_array($client[0]))){
          	$client=$client[0];
          	$client_tech=$client["technology"];
      			//$client_name=$client["name"]." ".$client["surname"]." &lt;".$client["extension"]."&gt";
            $client_name=$client["extension"];
      			$client_icon=getIcon($client_tech);
          	$html.="<li style=\"margin:0; padding:0\">$client_icon <a href=\"../extensions/edit/?id=".$client["id"]."\">".$client_name."</a></li>";
        	}
        }
        $html.="</ul>";
      }
      $html.="</td>\n";
      if ((++$count)%2==0) $html.="</tr><tr>\n";
    }
    $html.="<tr><td colspan=\"2\">&nbsp;</td></tr><tr>\n";
  }
  //get clients
  $ret=$asterisk->getClient(0,0);
  $count=0;
  if((is_array($ret))&&(count($ret)>0)){
  	$html.="<td colspan=\"2\" class=\"todolisttitle\">".T_("Extensions")."</td></tr><tr>\n";
    foreach($ret as $v){
      $id=$v["id"];
      //skip extensions that are contained in a virtual extension
    	if((is_array($blacklist))&&(in_array($id,$blacklist,true))){
      	continue;
      }
      $ext=$v["extension"];
      $tech=$v["technology"];
      $callerid=$v["name"]." ".$v["surname"];
      $icon=getIcon($tech);
      $html.="<td>$icon  <a href=\"edit/?id=$id\">$callerid &lt;$ext&gt;</a>  <a href=\"$PHP_SELF?cmd=del&id=$id\" onclick=\"if (confirm('".T_("Account:")." ".sprintf(T_("Are you sure you want to remove the extension %s"),$ext )."')) return true; else return false;\"><img src=\"$relBasePath/public/img/trash.gif\" /></a></td>\n";
      if ((++$count)%2==0) $html.="</tr><tr>\n";
    }
  }

  //================ MAIN CONTENTS ================
  ob_start();
  ?>
  <div class="SectionHeader">
    <?=$alert?>
    <h1><?=T_("Extensions")?></h1>
    <h3>
      <a href="edit/" class="admin" ><?=T_("New extension")?></a>
      <a style="margin-left:10px;" href="edit-virtual/" class="admin" ><?=T_("New virtual extension")?></a>
    </h3>
  </div>
  <table class="todolist">
    <tr>
      <?=$html?>
    </tr>
  </table>
  <?
  $contents=ob_get_contents();
  ob_end_clean();

  //================ SIDEBAR ================
  ob_start(); 
  ?>
    <div class="Sidebar">
      <?$sel="extensions";include("../sidebar.php");?>
      <h1><?=T_("Informations")?></h1>
    </div>
  <?  
  $sidebar=ob_get_contents();
  ob_end_clean();
  //================ SETTINGS ================

  $smarty->assign("section", "extensions");
  $smarty->assign("subsection", "");
  $smarty->assign("templateType", "TwoColLeft");  //OneCol, TwoColLeft, TwoColRight
  $smarty->assign("contents", $contents);
  $smarty->assign("sidebar", $sidebar);
  $smarty->display('settings.tpl');
?>

